package com.im.business.history.controller;

import com.im.business.history.api.HistoryMessageApi;
import com.im.business.history.domain.MessageHistoryVO;
import com.im.business.history.entity.MessageHistory;
import com.im.business.history.service.GroupMessageService;
import com.im.business.history.service.SingleMessageService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * @Created leijun
 * @Date 2021/11/2 8:38 上午
 */
@RestController
public class HistoryMessageController implements HistoryMessageApi {

    @Autowired
    private SingleMessageService singleMessageService;
    @Autowired
    private GroupMessageService groupMessageService;

    /**
     * 查询单聊历史消息
     * @param senderId
     * @param receiverId
     * @param maxOffset
     * @return
     */
    @Override
    public List<MessageHistoryVO> selectSingleHistoryMessage(Long senderId, Long receiverId, Long maxOffset) {
        List<MessageHistory> messageHistories =
                singleMessageService.selectHistoryMessage(senderId, receiverId, maxOffset);
        if (CollectionUtils.isEmpty(messageHistories)) {
            return null;
        }
        List<MessageHistoryVO> result = new ArrayList<>(messageHistories.size());
        for (MessageHistory messageHistory : messageHistories) {
            MessageHistoryVO messageHistoryVO = new MessageHistoryVO();
            BeanUtils.copyProperties(messageHistory, messageHistoryVO);
            result.add(messageHistoryVO);
        }
        return result;
    }

    /**
     * 查询群聊历史消息
     * @param groupId
     * @param maxOffset
     * @return
     */
    @Override
    public List<MessageHistoryVO> selectGroupHistoryMessage(Long groupId, Long maxOffset) {
        List<MessageHistory> messageHistories =
                groupMessageService.selectHistoryMessage(groupId, maxOffset);
        if (CollectionUtils.isEmpty(messageHistories)) {
            return null;
        }
        List<MessageHistoryVO> result = new ArrayList<>(messageHistories.size());
        for (MessageHistory messageHistory : messageHistories) {
            MessageHistoryVO messageHistoryVO = new MessageHistoryVO();
            BeanUtils.copyProperties(messageHistory, messageHistoryVO);
            result.add(messageHistoryVO);
        }
        return result;
    }
}
